package com.qf.mapper.impl;

import com.qf.mapper.StuInfoMapper;
import com.qf.pojo.StuInfo;
import com.qf.until.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 沐辰
 * @version v1.0
 * @description:
 * @project _4.30_Exam_two
 * @Date 2024/4/30 9:39
 * @company qf
 */
public class StuInfoMapperImpl implements StuInfoMapper {

    private QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());

    /**
     * 通过用户名查询相关信息
     * @param username
     * @return
     */
    @Override
    public StuInfo login(String username) {
        String sql = "select * from stuinfo where stuname = ?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(StuInfo.class),username);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 添加信息
     * @param stuInfo
     * @return
     */
    @Override
    public int insert(StuInfo stuInfo) {
        String sql = "insert into stuinfo(stuname,stuno,pwd,idcard,startdate,status) values(?,?,?,?,?,?)";
        try {
            return queryRunner.update(sql,stuInfo.getStuName(),stuInfo.getStuNo(),stuInfo.getPwd(),stuInfo.getIdCard()
                    ,stuInfo.getStartDate(),stuInfo.getStatus());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 计算总条数
     * @return
     */
    @Override
    public long count() {
        String sql = "select count(1) from stuinfo";
        try {
            return queryRunner.query(sql,new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 分页查询信息
     * @param offset
     * @param size
     * @return
     */
    @Override
    public List<StuInfo> listMsg(int offset, int size) {
        String sql = "select * from stuinfo limit ?,?";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(StuInfo.class),offset,size);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据id删除信息
     * @param id
     * @return
     */
    @Override
    public int delete(int id) {
        String sql = "delete from stuinfo where id = ?";
        try {
            return queryRunner.update(sql,id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据id查询信息
     * @param id
     * @return
     */
    @Override
    public StuInfo selectById(int id) {
        String sql = "select * from stuinfo where id = ?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(StuInfo.class),id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据id修改信息
     * @param stuInfo
     * @return
     */
    @Override
    public int updateById(StuInfo stuInfo) {
        String sql = "update stuinfo set stuname = ?,stuno = ?,pwd = ?,idcard = ?,startdate = ? where id = ?";
        try {
            return queryRunner.update(sql,stuInfo.getStuName(),stuInfo.getStuNo(),stuInfo.getPwd(),stuInfo.getIdCard(),
                    stuInfo.getStartDate(),stuInfo.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
